package mh;

import java.awt.Rectangle;

/**
 * The spell casted by any player.
 * 
 * @author Gianmarco Laggia
 * 
 */
public class SpellEntity extends Entity {

	private double	xSpeed;
	private double	ySpeed;

	/**
	 * Constructor, mouse coordinates are used to calculate the angle of the image and the couple of vertical and
	 * horizontal speeds needed to move it.
	 * 
	 * @param sprite
	 *            The path to the imageset
	 * @param plx
	 *            The player position (x) in the map (tile unit)
	 * @param ply
	 *            The player position (y) in the map (tile unit)
	 * @param mousex
	 *            X position of the mouse the very moment this spell is created.
	 * @param mousey
	 *            Y position of the mouse the very moment this spell is created.
	 */
	public SpellEntity(String sprite, double plx, double ply, double mousex, double mousey) {
		super(sprite, plx, ply, plx, ply);
		this.sprite = ResourceFactory.get().getSpell(sprite);

		double cosAlpha;
		double sinAlpha;
		mousex -= TILE_SIZE_DBL / 2;
		mousey -= TILE_SIZE_DBL / 2;
		double distance = Math.sqrt(Math.pow(mousey - CENTRAL_ROW_PX, 2) + Math.pow(mousex - CENTRAL_COLUMN_PX, 2));
		sinAlpha = (mousex - CENTRAL_COLUMN_PX) / distance;
		cosAlpha = (mousey - CENTRAL_ROW_PX) / distance;
		this.xSpeed = SPELL_SPEED * sinAlpha;
		this.ySpeed = SPELL_SPEED * cosAlpha;
		this.sprite.setDirection((int) Math.toDegrees(Math.atan2(mousey - CENTRAL_ROW_PX, mousex - CENTRAL_COLUMN_PX)));

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see mh.Entity#draw(double)
	 */
	@Override
	public void draw(long delta) {
		this.sprite.draw(((this.actualX + CENTRAL_COLUMN) - this.playerX) * 32, ((this.actualY + CENTRAL_ROW) - this.playerY) * 32);
		//		this.lastFrameChange += delta;
		//
		//		if (this.lastFrameChange > 40) {
		//			this.lastFrameChange = 0;
		//
		//		}
		this.actualX += ((delta * this.xSpeed) / 1000) / TILE_SIZE_DBL;
		this.actualY += ((delta * this.ySpeed) / 1000) / TILE_SIZE_DBL;
	}

	/**
	 * In the spell we do not need to move the entity, so the move method is useless.
	 */
	@Override
	public void move(long delta, int direction) {
		return;
	}

	/**
	 * Checks if the given entity collide with this spell.
	 * 
	 * @param other
	 * @return true if collided
	 */
	public boolean collidesWith(OtherEntity other) {
		Rectangle me = new Rectangle();
		Rectangle that = new Rectangle();
		me.setBounds((int) (this.actualX * 32) + 14, (int) (this.actualY * 32) + 14, 3, 3);
		that.setBounds((int) other.getX() * 32, (int) other.getY() * 32, 32, 32);
		return me.intersects(that);

	}

	/**
	 * TODO
	 */
	public void iWasHit() {
		this.xSpeed = 0;
		this.ySpeed = 0;
		this.actualX = 0;
		this.actualY = 0;
	}

}

//package mh;
//
//import java.awt.Rectangle;
//
//import mh.interfaces.IImage;
//
///**
// * This Entity represent the spell casted by a player.
// * 
// * @author Gianmarco Laggia
// * 
// */
//public class SpellEntity extends EntityOld {
//
//	private boolean	used	= false;
//	private final int	spellType;
//
//	/** The animation frames */
//	private final IImage[]	frames			= new Sprite[4];
//	/** The time since the last frame change took place */
//	private long			lastFrameChange;
//	/** The frame duration in milliseconds, i.e. how long any given frame of animation lasts */
//	private final long		frameDuration	= 20;
//	/** The current frame of animation being displayed */
//	private int				frameNumber;
//	private double				x;
//	private double				y;
//	private final int					centerXDelta;
//	private final int					centerYDelta;
//	private final int			spriteCenterX;
//	private final int			spriteCenterY;
//	private final MagicHogwarts	game;
//
//	/**
//	 * The constructor of the spell.
//	 * 
//	 * @param g
//	 *            The game running this map
//	 * 
//	 * @param ref
//	 *            The sprite of the spell
//	 * @param x
//	 *            The x starting position of the spell
//	 * @param y
//	 *            The y starting position of the spell
//	 * @param type
//	 *            The type of this spell. See the Enum
//	 */
//	public SpellEntity(MagicHogwarts g, String ref, int x, int y, int type) {
//		super(ref, x * TILE_SIZE, y * TILE_SIZE);
//		this.sprite = ResourceFactory.get().getTile(ref);
//		this.centerXDelta = (TILE_SIZE - this.sprite.getWidth()) / 2;
//		this.centerYDelta = (TILE_SIZE - this.sprite.getHeight()) / 2;
//		this.spriteCenterX = this.sprite.getWidth() / 2;
//		this.spriteCenterY = this.sprite.getHeight() / 2;
//		this.x = (x * TILE_SIZE) + this.centerXDelta;
//		this.y = (y * TILE_SIZE) + this.centerYDelta;
//		this.spellType = type;
//		this.game = g;
//	}
//
//	/**
//	 * Notification that this shot has collided with another entity
//	 * 
//	 * @param other
//	 *            The other entity
//	 */
//	@Override
//	public void collidedWith(EntityOld other) {
//
//		if (this.used) {
//			return;
//		}
//		/*
//		 * If the spell hit an object it does something. If the spell hit a player it does something else. If the spell
//		 * hit another spell it does something else else.
//		 */
//		// if (other instanceof AlienEntity) {
//		//
//		// game.removeEntity(this);
//		// game.removeEntity(other);
//		//
//		// game.notifyAlienKilled();
//		this.used = true;
//		// }
//	}
//
//	public void draw(double delta,double x, double y) {
//		this.sprite.draw((int) this.x, (int) this.y);
//		this.lastFrameChange += delta;
//
//		if (this.lastFrameChange > this.frameDuration) {
//			this.lastFrameChange = 0;
//
//		}
//		this.x += ((delta * this.dx) / 1000);
//		this.y += ((delta * this.dy) / 1000);
//	}
//
//	@Override
//	public void move(long delta,int direction) {
//		this.lastFrameChange += delta;
//
//		if (this.lastFrameChange > this.frameDuration) {
//			this.lastFrameChange = 0;
//
//		}
//		this.x += ((delta * this.dx) / 1000);
//		this.y += ((delta * this.dy) / 1000);
//	}
//
//	public void endPoint(int x, int y) {
//		x -= this.spriteCenterX;
//		y -= this.spriteCenterY;
//		if (x == this.x) {
//			if (y == this.y) {
//				return;
//			}
//			this.dx = 0;
//			this.dy = Math.signum(y - CENTRAL_ROW_PX) * SPELL_SPEED;
//			return;
//		}
//		double distance = Math.sqrt(Math.pow(y - this.y, 2) + Math.pow(x - this.x, 2));
//		double sinAlpha = (x - this.x) / distance;
//		double cosAlpha = (y - this.y) / distance;
//		this.dx = SPELL_SPEED * sinAlpha;
//		this.dy = SPELL_SPEED * cosAlpha;
//	}
//	
//	@Override

//
//	@Override
//	public void draw(double delta) {
//		// TODO Auto-generated method stub
//		
//	}
//
//}
